package pl.edu.agh.iosr.gamblingzone.action;

import static org.jboss.seam.ScopeType.EVENT;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.UserDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.validators.PasswordGenerator;

@Stateful
@Scope(EVENT)
@Name("recover")
public class PasswordRecoveryAction implements PasswordRecovery {
	
	private User user;

	/** The em. */
	@PersistenceContext
	private EntityManager em;

	/** The faces messages. */
	@In
	private FacesMessages facesMessages;
	
	@Logger
	private Log log;
	
	private String emailAddress;
	
	private String userLogin;
	
	private boolean changed;

	public void recover() {
		log.info(userLogin + " " + emailAddress);
		
		UserDAO userDao = new UserDAOBean();
		userDao.setEntityManager(em);
		
		user = userDao.findByLogin(userLogin);
		
		if((user != null) && emailAddress.equals(user.getEmail())){
			String password = PasswordGenerator.generatePassword();
			
			log.info("new password: " + password);
			
			if(!sendEmailToUser(password)){
				changed = false;
				facesMessages.add("Serwery sa przeciazone : ) Sprobuj pozniej.");
			}else{
				user.setPassword(password);
				em.persist(user);
				facesMessages.add("New password was sent. Check mail");
				changed = true;
			}
		}else{
			facesMessages.add("Invalid login name or email");
		}
	}
	
	private boolean sendEmailToUser(String pwd){
		SimpleEmail email = new SimpleEmail();
		boolean result = true;
		
		try {
			email.setHostName("smtp.agh.edu.pl");			
			email.addTo("kramarcz@student.agh.edu.pl", user.getFirstName() + " " + user.getSurname());
			email.setFrom("no-respone@gamblingzone.kicks-ass.org", "gambling.zone");
			email.setSubject("New password request");
			email.setMsg("This is your new password for gambling.zone" + pwd);
			email.send();
		} catch (EmailException e) {
			log.error("Email exception");
			result = false;
		}
		
		return result;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public boolean isChanged() {
		return changed;	
	}
	
	public void invalid() {
		facesMessages.add("Please try again");
	}

	@Destroy
	@Remove
	public void destroy() {		
	}
}
